package com.wayne.blog.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wayne.blog.entity.Comment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 评论Mapper接口
 */
@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    
    /**
     * 根据文章ID查询顶级评论列表
     */
    @Select("SELECT c.*, u.nickname as user_nickname, u.avatar as user_avatar, " +
            "ru.nickname as reply_user_nickname, ru.avatar as reply_user_avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "LEFT JOIN user ru ON c.reply_user_id = ru.id " +
            "WHERE c.article_id = #{articleId} AND c.parent_id IS NULL AND c.deleted = 0 " +
            "ORDER BY c.create_time DESC")
    List<Comment> findTopCommentsByArticleId(Long articleId);

    /**
     * 根据父评论ID查询子评论列表
     */
    @Select("SELECT c.*, u.nickname as user_nickname, u.avatar as user_avatar, " +
            "ru.nickname as reply_user_nickname, ru.avatar as reply_user_avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "LEFT JOIN user ru ON c.reply_user_id = ru.id " +
            "WHERE c.parent_id = #{parentId} AND c.deleted = 0 " +
            "ORDER BY c.create_time ASC")
    List<Comment> findChildrenByParentId(Long parentId);
} 